﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.Serialization;

namespace TakePhotoTogether.Model
{
    [DataContract]
    public class SyncPictureQueue
    {
        /// <summary> Identification of user </summary>
        public string UserIdentifier;

        public Action<string> OnNewRemotePictureTaken = null;
        public Action<string> OnNewRemotePictureReceived = null;

        /// <summary> Queue of pictures to be synced </summary>
        public readonly Queue<string> picturePathList = new Queue<string>();

        /// <summary> List of unconfirmed pictures </summary>
        private readonly List<string> photosToBeConfirmed = new List<string>();

        private const string DEBUG_TAG = "TakePhotoTogether.Model.SyncPictureQueue => ";

        /// <summary>
        /// Reload unconfirmed pictures back to sync queue 
        /// </summary>
        public void Reload()
        {
            Debug.WriteLine(DEBUG_TAG + "Reload - adding {0} images to sync queue", photosToBeConfirmed.Count);

            foreach (string photo in photosToBeConfirmed)
            {
                
                picturePathList.Enqueue(photo);
            }

            photosToBeConfirmed.Clear();
        }

        /// <summary>
        /// Enqueue a photo to be synced
        /// </summary>
        /// <param name="photoName"></param>
        public void Enqueue(string photoName)
        {
            picturePathList.Enqueue(photoName);
        }

        /// <summary>
        /// Dequeue a photo to be synced
        /// </summary>
        /// <returns></returns>
        public string Dequeue()
        {
            string photoName = "";

            if (picturePathList.Count > 0)
            {
                photoName = picturePathList.Dequeue();
                photosToBeConfirmed.Add(photoName);
            }

            return photoName;
        }

        /// <summary>
        /// Confirm a photo as already synced
        /// </summary>
        /// <param name="photoName"></param>
        public void ConfirmedPhoto(string photoName)
        {
            photosToBeConfirmed.Remove(photoName);
        }

        /// <summary> Number of pictures to be synced. </summary>
        public int Count { get { return picturePathList.Count; } }

        public StorageQueue ToStorageQueue()
        {
            StorageQueue queue = new StorageQueue();

            queue.pictures = new string[photosToBeConfirmed.Count + picturePathList.Count];

            int c = 0;

            foreach (string pic in picturePathList)
            {
                queue.pictures[c] = pic;
                c++;
            }

            foreach (string pic in photosToBeConfirmed)
            {
                queue.pictures[c] = pic;
                c++;
            }

            return queue;
        }

        public static SyncPictureQueue FromStorageQueue(StorageQueue pictures, string identifier)
        {
            if (pictures == null || pictures.pictures == null)
                return null;

            SyncPictureQueue queue = new SyncPictureQueue();
            queue.UserIdentifier = identifier;

            foreach (string pic in pictures.pictures)
            {
                queue.Enqueue(pic);
            }

            return queue;
        }
    }
}
